﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/25/2009
 * Time: 9:50 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Miming.Model;
using Miming.Util;

namespace Miming.Gui
{
	/// <summary>
	/// Description of UserForm.
	/// </summary>
	public partial class UserForm : BaseForm
	{
		public UserForm() : this(new User())
		{
			
		}
		
		public UserForm(User u)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			SwfUtility.SetupComboBox(comboBoxModule, SessionFacade.FindAllModules(), "Id", "Name");
			Model = u;
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			User u = Model as User;
			textBoxName.Text = u.Name;
			textBoxPassword.Text = u.Password;
		}
		
		void ButtonOkClick(object sender, EventArgs e)
		{
			User u = Model as User;
			u.Name = textBoxName.Text;
			u.Password = textBoxPassword.Text;
			SessionFacade.SaveOrUpdateUser(u);
			Close();
		}
		
		void ButtonCancelClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void ComboBoxModuleSelectedIndexChanged(object sender, EventArgs e)
		{
			Module m = (comboBoxModule.DataSource as IList<Module>)[comboBoxModule.SelectedIndex];
			listViewAvailablePrivileges.Items.Clear();
			foreach (Privilege p in m.Privileges) {
				ListViewItem li = listViewAvailablePrivileges.Items.Add(p.Name);
			}
		}
	}
}
